package com.passer.network;


import com.passer.toolbox.common.core.util.StringUtils;
import com.passer.toolbox.common.network.util.IPUtils;
import com.passer.toolbox.common.network.util.NetworkUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.SocketException;
import java.util.Optional;
import java.util.Scanner;

@Slf4j
public class Launcher {
    public static void main(String[] args) {
        System.out.println("***************欢迎使用IPV4工具***************");
        // todo 显示电脑连接的是WIFI还是网线
        String method = null;
        try {
            method = IPUtils.getIPV4Method();
        } catch (IOException e) {
            System.err.println("查询本机当前获取IPV4方式失败：" + e.getMessage());
        }
        if (StringUtils.isNotEmpty(method)) {
            System.out.println("获取IPV4方式："+method);
        }
        Optional<Inet4Address> ip4Address = Optional.empty();
        try {
            ip4Address = NetworkUtils.getLocalIp4Address();
        } catch (SocketException e) {
            System.err.println("获取本机当前使用IPV4失败：" + e.getMessage());
        }
        ip4Address.ifPresent(inet4Address -> System.out.println("IPV4地址：" + inet4Address.getHostAddress()));

        Scanner scanner = new Scanner(System.in);
        int chose;
        while (true) {
            System.out.println("1.设置外网静态IP    2.设置外网随机静态IP    3.设置内网静态IP    4.设置内网随机静态IP");
            System.out.println("5.设置自动获取IP    6.退出");
            System.out.print("选择功能：");
            chose = scanner.nextInt();
            try {
                switch (chose) {
                    case 1:
                        System.out.println("输入要设置的IP和默认网关（例：127.0.0.1 127.0.0.255）:");
                        if (IPUtils.setStaticIPV4(scanner.next(), scanner.next(), false)) {
                            System.out.println("设置成功");
                        } else {
                            System.out.println("设置失败，请检查输入，或以管理员身份启动");
                        }
                        break;
                    case 2:
                        System.out.println("输入网关:");
                        if (IPUtils.setRandomIP(scanner.next(), false)) {
                            System.out.println("设置成功");
                        } else {
                            System.out.println("设置失败，请检查输入，或以管理员身份启动");
                        }
                        break;
                    case 3:
                        System.out.println("输入要设置的IP和默认网关（例：127.0.0.1 127.0.0.255）:");
                        if (IPUtils.setStaticIPV4(scanner.next(), scanner.next(), true)) {
                            System.out.println("设置成功");
                        } else {
                            System.out.println("设置失败，请检查输入，或以管理员身份启动");
                        }
                        break;
                    case 4:
                        System.out.println("输入网关:");
                        if (IPUtils.setRandomIP(scanner.next(), true)) {
                            System.out.println("设置成功");
                        } else {
                            System.out.println("设置失败，请检查输入，或以管理员身份启动");
                        }
                        break;
                    case 5:
                        if (IPUtils.setDynamicIPV4(false)) {
                            System.out.println("设置成功");
                        } else {
                            System.out.println("设置失败，请以管理员身份启动");
                        }
                        break;
                    case 6:
                        scanner.close();
                        return;
                    default:
                        System.out.println("输入正确的数字好吗？");
                }
            } catch (IOException e) {
                log.error("操作失败：{}", e.getMessage());
            }
        }
    }
}
